package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    /**
     * 根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，并获取自动生成的订单编号，
     * 然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，​
     * insert into orderDetailet
     * (SELECT
     * from cart c,business b,food f,user u
     * where c.businessId=b.businessId and c.foodId = f.foodId and c.userId = u.userId and c.userId=? and b.business=?)
     * 然后根据用户编号、商家编号删除购物车表中的数据。

     * @return
     * @throws Exception
     */
    @Override
    public int createOrders(Orders orders) throws Exception {
        Integer rs = 1;
        String sql = "insert into orders values(null,?,?,?,?,?,0)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,orders.getUserId());
            pst.setInt(2,orders.getBusinessId());
            pst.setString(3,orders.getOrderDate());
            pst.setDouble(4,orders.getOrderTotal());
            pst.setInt(5,orders.getDaId());
            rs = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return rs;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders orders = null;
        String sql = "select o.*,d.odId odId,d.foodId foodId,d.quantity quantity,b.businessName businessName,b.businessAddress businessAddress,b.businessExplain businessExplain," +
                        "b.businessImg businessImg,b.orderTypeId orderTypeId,b.starPrice starPrice,b.deliveryPrice deliveryPrice,b.remarks remarks " +
                     "from (orders o left join business b on o.businessId=b.businessId) left join orderdetailet d on o.orderId=d.orderId " +
                     "where o.orderId=?";
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderId);
            rs = pst.executeQuery();
            while (rs.next()){
                orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        List<Orders> list = new ArrayList<>();
        String sql = "select b.*,d.*,o.*" +
                "from (orders o left join business b on o.businessId=b.businessId) left join " +
                "orderdetailet d on o.orderId=d.orderId " +
                "where o.userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,userId);
            rs = pst.executeQuery();
            while (rs.next()){
                Orders orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                orders.setBusiness(business);
                list.add(orders);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}
